package org.tema.ejb;

import java.util.Collection;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.tema.entities.Product;
import org.tema.entities.User;


/**
 * Session Bean implementation class ProductBean
 */
@Stateless
public class ProductBean implements ProductBeanLocal {


	
	@PersistenceContext
	private EntityManager entityManager;

	@EJB
	UserBeanLocal userBean;
	
	
    public ProductBean() {
        // TODO Auto-generated constructor stub
    }

	public Product addProduct( String name, String description, String version, String creatorName ){
    	User currentUser = userBean.findUserByName(creatorName);
		Product beanProduct = new Product ();
		beanProduct.setName(name);
		beanProduct.setDescription(description);
		beanProduct.setVersion(version);
		beanProduct.setCreator(currentUser);
		entityManager.persist( beanProduct);
		return beanProduct;
	}
	
	public Product updateProduct(Product product, String modifierName){
    	User currentUser = userBean.findUserByName(modifierName);
    	product.setModifier(currentUser);
	    entityManager.merge(product);
	    return product;
	}
	
	public Product undoProductChanges(Product product){
	    entityManager.refresh(entityManager.merge(product));
	    return product; 
	}
	
	public void deleteProduct(Product product){
		entityManager.remove(entityManager.merge(product));
	}
	
	//should return a list of Products
	public Product findProductByName( String name ){
		Query query;
		Product product;
		try{
			query = entityManager.createQuery(" SELECT p FROM Product p WHERE p.name = :name  ");
			query.setParameter("name", name);
			product = (Product) query.getSingleResult();
		}
		catch (NoResultException e) {
			throw new IllegalArgumentException("no product with name '" + name + "'");
		}
		return product;	
	}
	
	@SuppressWarnings("unchecked")
	public Collection <Product> findProductsByName( String name ){
		Query query;
		Collection <Product> products;
		try{
			name = name .concat("%");
			query = entityManager.createQuery(" SELECT p FROM Product p WHERE p.name LIKE :name  ");
			query.setParameter("name", name);
			products = query.getResultList();
		}
		catch (NoResultException e) {
			throw new IllegalArgumentException("no product with name '" + name + "'");
		}
		return products;	
	}
	
	public Product  findProductById( int id ){
		Query query;
		Product product;
		try{
			query = entityManager.createQuery(" SELECT p FROM Product p WHERE p.id = :id  ");
			query.setParameter("id", id);
			product = (Product) query.getSingleResult();
		}
		catch (NoResultException e) {
			throw new IllegalArgumentException("no product with id '" + id + "'");
		}
		return product;	
	}
	
	//should return a list of Products
	@SuppressWarnings("unchecked")
	public Collection <Product>  findProductByVersion( String version ){
		version= version.concat("%");
		Query query = entityManager.createQuery(" SELECT p FROM Product p WHERE p.version LIKE :version  ");
		query.setParameter("version", version);
		return (Collection <Product> ) query.getResultList();
	}
	

	
	@SuppressWarnings("unchecked")
	public Collection <Product> findAllProducts(){
		Query query = entityManager.createQuery(" SELECT p FROM Product p ");
		return query.getResultList();
	}
    
}
